//
//  BoZeMeVC.swift
//  BoZeCalendar
//
//  Created by 李东 on 2024/4/11.
//

import UIKit
import SafariServices

class BoZeMeVC: BoZeBaseVC {
    lazy var headerView = UIImageView(image: UIImage(named: "img_me_header"))
    lazy var iconImgView = UIImageView(image: UIImage(named: "img_wangguan"))
    lazy var dateLabel = UILabel()
    lazy var vipImageView = UIImageView(image: UIImage(named: "img_vip_text"))
    lazy var openVipBtn = UIButton()
    lazy var contentView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()

        hideNavgaitionBar()
        showBgImgView = true
        bgImgView.image = UIImage(named: "img_me_bg")
        showPopBtn = true
        navTitle = "我的"
        initHeader()
        configContentUI()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        reloadHeader()
        if AppProducts.shared.isPurchased {
            adHelper.removeBannerView()
        }else{
            if hasLoadAd {
                return
            }
            hasLoadAd = true
            adHelper.loadBanner(currentVC: self, isMainVC: false)
        }
    }
    
    @objc private func reloadHeader() {
        if AppProducts.shared.isPurchased  {
            openVipBtn.isHidden = true
            vipImageView.isHidden = false
            dateLabel.text = String(format: "%@%@", NSLocalizedString("会员有效期至:", comment: ""),DateFormatter.formatter.string(from: AppProducts.shared.subscriptionExpirationDate ?? Date()))
        }else{
            openVipBtn.isHidden = false
            vipImageView.isHidden = true
            dateLabel.text = ""
        }

    }
    
    private func initHeader() {
        headerView.contentMode = .scaleAspectFill
        headerView.isUserInteractionEnabled = true
        view.addSubview(headerView)
        headerView.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(CGFloat.navHeight + 20)
            make.height.equalTo(headerView.snp.width).multipliedBy(0.449275362)
        }
        
        headerView.addSubview(iconImgView)
        iconImgView.snp.makeConstraints { make in
            make.left.top.equalTo(15)
        }
        
        dateLabel.textColor = .black
        dateLabel.font = UIFont.systemFont(ofSize: 14)
        headerView.addSubview(dateLabel)
        dateLabel.snp.makeConstraints { make in
            make.left.equalTo(iconImgView.snp.right).offset(10)
            make.centerY.equalTo(iconImgView).offset(2)
        }
        
        openVipBtn.setImage(UIImage(named: "img_open_vip"), for: .normal)
        openVipBtn.addTarget(self, action: #selector(openVip), for: .touchUpInside)
        headerView.addSubview(openVipBtn)
        openVipBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(10)
        }
        
        headerView.addSubview(vipImageView)
        vipImageView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(10)
        }
        
    }
    
    func configContentUI() {
        contentView.backgroundColor = .white
        contentView.layer.cornerRadius = 15
        contentView.layer.masksToBounds = true
        view.addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(headerView.snp.bottom).offset(30)
            make.height.equalTo(121)
        }
        
        let serviceView = UIView()
        serviceView.tag = 10
        serviceView.isUserInteractionEnabled = true
        serviceView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(agreementClick(sender:))))
        contentView.addSubview(serviceView)
        serviceView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(60)
        }
        
        let serviceLabel = UILabel()
        serviceLabel.textColor = .black
        serviceLabel.font = UIFont.systemFont(ofSize: 15)
        serviceLabel.text = "服务协议"
        serviceView.addSubview(serviceLabel)
        serviceLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(15)
        }
        
        let arrow1 = UIImageView(image: UIImage(named: "arrow_black"))
        serviceView.addSubview(arrow1)
        arrow1.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-15)
        }
        
        let line = UIView()
        line.backgroundColor = .black.withAlphaComponent(0.2)
        contentView.addSubview(line)
        line.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.centerY.equalToSuperview()
            make.height.equalTo(1)
        }
        
        let privacyView = UIView()
        privacyView.tag = 20
        privacyView.isUserInteractionEnabled = true
        privacyView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(agreementClick(sender:))))
        contentView.addSubview(privacyView)
        privacyView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(60)
        }
        
        let privacyLabel = UILabel()
        privacyLabel.textColor = .black
        privacyLabel.font = UIFont.systemFont(ofSize: 15)
        privacyLabel.text = "隐私协议"
        privacyView.addSubview(privacyLabel)
        privacyLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(15)
        }
        
        let arrow2 = UIImageView(image: UIImage(named: "arrow_black"))
        privacyView.addSubview(arrow2)
        arrow2.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-15)
        }
        
        
    }
    

    @objc func agreementClick(sender: UIGestureRecognizer) {
        guard let tempView = sender.view else { return  }
        switch tempView.tag {
        case 10:
            if let tempUrl = URL(string: AppConfig.SERVICE_URL) {
                present(SFSafariViewController(url: tempUrl), animated: true)
            }
            break
        default:
            if let tempUrl = URL(string: AppConfig.PRIVACY_URL) {
                present(SFSafariViewController(url: tempUrl), animated: true)
            }
            break
        }
    }

    @objc func openVip() {
        showSubVC()
    }

}
